<?php
class UserController extends AppController
{
    public $name = 'User';
    public $helpers = array('Html', 'Form');
    public $components = array('Session');
    public $uses = array(
        'tbl_account', 'tbl_character', 'tbl_billing_log', 'tbl_item', 'tbl_card',
    );

    public function user_list()
    {
        $user_list = '';
        $search_info = '';

        if(!empty($this->data['search_user']))
        {
            $search_info = $this->data['search_user'];

            if(!empty($search_info['nickname']))
            {
                $search_info = $this->data['search_user'];

                if(!$user_list = $this->tbl_account->search_nickname($search_info['nickname']))
                {
                    $err_msg = LANG_USER_EMPTY;
                }
            }
            else
            {
                $err_msg = LANG_INPUT_NICKNAME;
            }
        }

        $this->set(compact('user_list', 'search_info', 'err_msg'));
    }

    public function user_select()
    {
        $id = $this->passedArgs['id'];
        $user_data = $this->tbl_account->get_account($id);
        $user_character_list = $this->tbl_character->get_character($id);
        $user_billing_list = $this->tbl_billing_log->get_billing_list($id);

        if(!empty($this->passedArgs['char_serial']))
        {
            $char_serial = $this->passedArgs['char_serial'];
            $char_item_list = $this->__user_char_item($char_serial);
            $char_card_list = $this->__user_char_card($char_serial);
        }

        $this->set(compact(
            'user_data', 'user_character_list', 'user_billing_list','char_item_list',
            'char_card_list', 'char_serial'
        ));
    }

    public function user_select_edit()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        $id = $this->passedArgs['id'];
        $user_data = $this->tbl_account->get_account($id);
        $user_character_list = $this->tbl_character->get_character($id);

        if( !empty($this->data['edit_info']) )
        {
            $edit_info = $this->data['edit_info'];

            if( nullCheck($edit_info['gem']) )
            {
                $err_msg = __('Space is empty!');
            }
            else if( !numberCheck($edit_info['gem']) )
            {
                $err_msg = __("Numeric only!");
            }
            else
            {
                $user_data['tbl_account']['gem'] = $edit_info['gem'];
                $user_data['tbl_account']['block'] = $edit_info['block'];
                $this->tbl_account->save($user_data);

                if(!empty($this->passedArgs['char_serial']))
                {
                    if( nullCheck($edit_info['energy']) || nullCheck($edit_info['exp']) || nullCheck($edit_info['gold']) )
                    {
                        $err_msg = __('Space is empty!');
                    }
                    else if( !numberCheck($edit_info['energy']) || !numberCheck($edit_info['exp']) || !numberCheck($edit_info['gold']) )
                    {
                        $err_msg = __("Numeric only!");
                    }
                    else
                    {
                        $char_serial = $this->passedArgs['char_serial'];

                        $user_character['tbl_character']['a2x_id'] = $edit_info['a2x_id'];
                        $user_character['tbl_character']['char_serial'] = $char_serial;
                        $user_character['tbl_character']['gold'] = $edit_info['gold'];
                        $user_character['tbl_character']['energy'] = $edit_info['energy'];
                        $user_character['tbl_character']['exp'] = $edit_info['exp'];
                        $this->tbl_character->save($user_character);
                    }
                }

                $this->redirect(array(
                    'controller' => 'User',
                    'action' => 'user_select',
                    'id' => $id,
                    'search_value' => $this->passedArgs['search_value'],
                    'char_serial' => empty($char_serial) ? '' : $char_serial,
                ));
            }
        }

        $this->set(compact(
            'user_data', 'user_character_list', 'char_serial', 'err_msg'
        ));
    }

    public function slide_user_search()
    {
        $search_value = CakeSession::read('search_value');
        $user_list = $this->tbl_account->search_nickname($search_value);

        return $user_list;
    }

    private function __user_char_card($_char_serial)
    {
        $char_item_list = $this->tbl_card->get_card_list($_char_serial);
        return $char_item_list;
    }

    private function __user_char_item($_char_serial)
    {
        $char_item_list = $this->tbl_item->get_item_list($_char_serial);
        return $char_item_list;
    }

}